package com.prs.crm.service.cdxh.impl;

import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.prs.crm.dao.cdxh.PrintingTeamDao;
import com.prs.crm.domain.cdxh.Cdstock;
import com.prs.crm.domain.cdxh.CrmCustomer;
import com.prs.crm.domain.cdxh.DistributionOrder;
import com.prs.crm.domain.cdxh.DistributionOrderDetail;
import com.prs.crm.domain.cdxh.HrPerson;
import com.prs.crm.domain.cdxh.SaleOrderDetail;
import com.prs.crm.domain.cdxh.SerialNumberss;
import com.prs.crm.domain.cdxh.StockPrecautionary;
import com.prs.crm.domain.cdxh.cd_Product;
import com.prs.crm.domain.cdxh.printing.PrintPrintIng;
import com.prs.crm.domain.cdxh.printing.PrintPrintIngDetail;
import com.prs.crm.domain.cdxh.printing.Printing;
import com.prs.crm.domain.cdxh.printing.PrintingTeam;
import com.prs.crm.domain.cdxh.production.ProvideRegime;
import com.prs.crm.domain.cdxh.wages.PrintWages;
import com.prs.crm.service.cdxh.CdstockService;
import com.prs.crm.service.cdxh.CrmCustomerService;
import com.prs.crm.service.cdxh.DistributionOrderDetailService;
import com.prs.crm.service.cdxh.DistributionOrderService;
import com.prs.crm.service.cdxh.EarningSalService;
import com.prs.crm.service.cdxh.HrPersonService;
import com.prs.crm.service.cdxh.PrintPrintIngService;
import com.prs.crm.service.cdxh.PrintWagesService;
import com.prs.crm.service.cdxh.PrintingService;
import com.prs.crm.service.cdxh.PrintingTeamService;
import com.prs.crm.service.cdxh.ProvideRegimeService;
import com.prs.crm.service.cdxh.SaleOrderService;
import com.prs.crm.service.cdxh.SerialNumberssService;
import com.prs.crm.service.cdxh.StockPrecautionaryService;
import com.prs.crm.service.cdxh.cd_ProductService;
import com.prs.crm.service.impl.BaseServiceImpl;

/**
 * @author LanXiaoGang
 * @E-mail:lan9liang@163.com
 * @qq:7857553326
 * @version 创建时间：2010-12-9 下午02:51:04 简单说明
 */

public class PrintingTeamServiceImpl extends
		BaseServiceImpl<PrintingTeam, PrintingTeamDao> implements
		PrintingTeamService {
	
	private PrintWagesService printWagesService;
	
	private EarningSalService earningSalService;

	private Cdstock cdstock;

	private CdstockService cdstockService;

	private DistributionOrder distributionOrder;

	private DistributionOrderService distributionOrderService;

	private StockPrecautionaryService stockPrecautionaryService;

	private SerialNumberssService serialNumberssService;

	private PrintPrintIng printPrintIng;

	private PrintPrintIngService printPrintIngService;

	private PrintingTeam printingTeam;

	private PrintingTeamService printingTeamService;

	private Printing printing;

	private PrintingService printingService;

	private ProvideRegimeService provideRegimeService;

	private DistributionOrderDetailService distributionOrderDetailService;

	private ProvideRegime provideRegime;

	private HrPerson hrPerson;

	private HrPersonService hrPersonService;
	
	private CrmCustomerService crmCustomerService;
	
	private cd_ProductService cdProductService;

	private SaleOrderService saleOrderService;
	@Override
	public void saveALLPrintingTeam(Integer[] feiliao,PrintingTeam printingTeam,
			String[] print_size, String[] print_color, String[] packway,
			Integer[] print_order_numg, Integer[] print_articles,
			Integer[] print_shiji_num, String[] print_fangxian,
			String[] print_jianxian, String[] print_baozhuang,
			String[] print_customer, String[] print_shijian,
			String[] print_remark, Integer[] firstYN, String[] printOrderPlans,
			String[] printCodes, Date[] tihuodates, String[] rushOrders) {
		double wages = 0.0;
		double funds = 0.0;
		int insV=0;
	
		int lengths = print_shiji_num.length;
		for (int i = 0; i < lengths; i++) {
			Printing ptObject = this.getPrintingService().getPrinting(
					printCodes[i], printOrderPlans[i]);
			int firstYNs = ptObject.getFirstYN();
			if (firstYNs == 1) {
				// 是第一次
				if (print_shiji_num[i] >= print_articles[i]) {
					ptObject.setBanChengNum(0);
					ptObject.setBanShenyuNum(print_shiji_num[i]-feiliao[i]);
					ptObject.setFirstYN(2);
					ptObject.setPrintShowState(7);
				} else {
					ptObject.setBanChengNum(ptObject.getBanChengNum()
							- print_shiji_num[i]);
					ptObject.setBanShenyuNum(print_shiji_num[i]-feiliao[i]);
					ptObject.setFirstYN(2);
				}

			} else {
				// 不是第一次
				if (print_shiji_num[i] >= print_articles[i]) {
					ptObject.setBanChengNum(0);
					ptObject.setBanShenyuNum(ptObject.getBanChengNum()
							+ print_shiji_num[i]-feiliao[i]);
					ptObject.setFirstYN(2);
					ptObject.setPrintShowState(7);
				} else {
					ptObject.setBanChengNum(ptObject.getBanChengNum()
							- print_shiji_num[i]);
					ptObject.setBanShenyuNum(print_shiji_num[i]-feiliao[i]);
					ptObject.setFirstYN(2);
				}

			}
			this.getPrintingService().saveOrPrinting(ptObject);

			PrintingTeam ptobject = new PrintingTeam();
			ptobject.setPlanOrderCode(printOrderPlans[i]);
			ptobject.setPrintRushOrser(rushOrders[i]);
			ptobject.setPrintTihuoDate(tihuodates[i]);
			ptobject.setPrintCode(printCodes[i]);
			ptobject.setPrintMachine(printingTeam.getPrintMachine());
			ptobject.setPrintClass(printingTeam.getPrintClass());
			ptobject.setPrintDate(printingTeam.getPrintDate());
			ptobject.setPrintSize(print_size[i]);
			ptobject.setPrintColor(print_color[i]);
			ptobject.setPrintPackWay(packway[i]);
			ptobject.setPrintOrderNum(print_order_numg[i]);
			ptobject.setPrintBanchenNum(print_articles[i]);
			ptobject.setPrintYSNum(print_shiji_num[i]);
			ptobject.setPrintFanfanxian(print_fangxian[i]);
			ptobject.setPrintJianxian(print_jianxian[i]);
			ptobject.setPrintBaozhuang(print_baozhuang[i]);
			ptobject.setPrintCustomer(print_customer[i]);
			ptobject.setPrintchanDate(print_shijian[i]);
			ptobject.setPrintRemark(print_remark[i]);
			ptobject.setPrintCDateA(printingTeam.getPrintCDateA());
			ptobject.setPrintCDateB(printingTeam.getPrintCDateB());
			ptobject.setPrintCDateC(printingTeam.getPrintCDateC());
			ptobject.setPrintCDateD(printingTeam.getPrintCDateD());
			ptobject.setPrintCDateE(printingTeam.getPrintCDateE());
			ptobject.setPrintCDateF(printingTeam.getPrintCDateF());
			ptobject.setPrintCDateG(printingTeam.getPrintCDateG());
			ptobject.setPrintCDateH(printingTeam.getPrintCDateH());
			ptobject.setPrintWorkState(printingTeam.getPrintWorkState());
			ptobject.setPrintRemarks(printingTeam.getPrintRemarks());
			ptobject.setPrintZhuG(printingTeam.getPrintZhuG());
			ptobject.setPrintJishuYuan(printingTeam.getPrintJishuYuan());
			ptobject.setPrintTianBiao(printingTeam.getPrintTianBiao());
			ptobject.setPrintfeiliao(feiliao[i]);//废料
			this.getPrintingTeamService().save(ptobject);

			/**
			 * 对库房预警进行设置
			 */
			Cdstock stockObject = this.getCdstockService().getCdstock(
					printCodes[i], print_customer[i]);
			if (stockObject != null) {
				int max_Nums = stockObject.getMax_amount();
				int min_Nums = stockObject.getMin_amount();
				int amount = stockObject.getAmount() + print_shiji_num[i]-feiliao[i];

				if (amount <= max_Nums && amount >= min_Nums) {

					stockObject.setAmount(stockObject.getAmount()
							+ print_shiji_num[i]-feiliao[i]);
					this.getCdstockService().saveOrStock(stockObject);
				} else {
					StockPrecautionary spobject = this
							.getStockPrecautionaryService()
							.getStockPrecautionaryByProductId(printCodes[i]);
					if (spobject != null) {
						spobject.setProductNum(spobject.getProductNum()
								+ print_shiji_num[i]-feiliao[i]);
						this.getStockPrecautionaryService()
								.saveOrStockPrecautionary(spobject);
					} else {
						StockPrecautionary spkk = new StockPrecautionary();
						spkk.setProductCode(printCodes[i]);
						spkk.setProductDownline(stockObject.getMin_amount());
						spkk.setProductUpline(stockObject.getMax_amount());
						spkk.setProductNum(amount);
						spkk.setProductName(ptObject.getPrintName()); //产品名称
						SaleOrderDetail detail=this.saleOrderService.getSaleOrderDetailByproductCode(printOrderPlans[i]);
						CrmCustomer crm=this.crmCustomerService.getCrmCustomerBycusCode(detail.getSaleOrder().getCusCode());
						spkk.setCrmcustomer(crm);       //客户名称
						spkk.setPrTitle("安全库存");
						this.getStockPrecautionaryService().save(spkk);
					}
					stockObject.setAmount(stockObject.getAmount()
							+ print_shiji_num[i]-feiliao[i]);
					this.getCdstockService().saveOrStock(stockObject);
				}
			} else {

			}

			/**
			 * 对配送单进行设置
			 */
			
			DistributionOrder dbObject = this.getDistributionOrderService()
					.setDistributionOrder(replaceCodes(printOrderPlans[i]));
			if (dbObject != null) {
				DistributionOrderDetail dbDetail = this
						.getDistributionOrderDetailService()
						.setDistributionOrderDetail(replaceCodes(printOrderPlans[i]),
								printCodes[i]);
				if (dbDetail != null) {
					dbDetail.setProToNums(dbDetail.getProToNums()+print_shiji_num[i]-feiliao[i]);
					this.getDistributionOrderService()
							.saveOrDistributionOrderDetail(dbDetail);
				}
				dbObject.setState("0");
				this.getDistributionOrderService().saveOrDistributionOrder(
						dbObject);
			}

		}
		/**
		 * 工资单计算-------------------satrt
		 */
	
		if(insV==0){
			for(int x=0;x<lengths;x++){
				//规格
				String norms[]=print_size[x].split("\\*");
				wages = wages + Double.parseDouble(((Double.parseDouble(norms[1])/1000)*(print_shiji_num[x]-feiliao[x]))+"");;
				
			}
		/**
		 * 人员比对-----------------start-------
		 */
		// 每个人本次制定分到的实际数量
		double shiji_merNum = 0.0;
		// 每个人本次制定分到的 废料数量
		double feiliao_merNum = 0.0;
		String personNames = printingTeam.getPrintJishuYuan();
		String lengthss[] = personNames.split(",");
		int personlength = lengthss.length;
		DecimalFormat df = new DecimalFormat("##.000");
		if (personlength > 0) {
			// 大于一个人，需要平分
			shiji_merNum = Double.parseDouble(df.format(wages / personlength));
			
		} else {
			// 一个人独享
			
			shiji_merNum = wages;
			
		}

		/**
		 * 人员比对------------------end------
		 */
		/**
		 * 工资单计算------------------------------------------------------start
		 * wages funds
		 */
		ProvideRegime pdobjectA = this.getProvideRegimeService()
		.getProvideRegime("印刷", "实际生产量",
				printingTeam.getPrintClass(),
				printingTeam.getPrintMachine());
double shijichanliangNum = (double) pdobjectA.getSetNum();
double jiangliDJ = pdobjectA.getRewardPrice();
double chengfaDJ = pdobjectA.getPunishPrice();
String uuDate=getCurrentDate();
for (int z = 0; z < personlength; z++) {
	PrintWages  mwObject=new PrintWages();
	mwObject.setProvideNums(shijichanliangNum+"");
	mwObject.setProNames(lengthss[z]);
	mwObject.setPoundPrice(jiangliDJ);
	mwObject.setRwardPrice(chengfaDJ);
	mwObject.setRegDate(uuDate);
	mwObject.setThNums(shiji_merNum);//实际印刷数量
	mwObject.setDiscriminationValue(pdobjectA.getDiscriminationValue());
	this.getPrintWagesService().save(mwObject);
	
}

insV++;
		}

//		ProvideRegime pdobjectA = this.getProvideRegimeService()
//				.getProvideRegime("印刷", "实际生产量", printingTeam.getPrintClass(),
//						printingTeam.getPrintMachine());
//		// System.out.println("printingTeam.getPrintClass()="
//		// + printingTeam.getPrintClass());
//		// System.out.println("printingTeam.getPrintMachine()="
//		// + printingTeam.getPrintMachine());
//		// System.out.println("对象=" + pdobjectA);
//		double shijichanliangNum = (double) pdobjectA.getSetNum();
//		double jiangliDJ = pdobjectA.getRewardPrice();
//		double chengfaDJ = pdobjectA.getPunishPrice();
//		double jiangliM = 0.0;
//		double chengfaM = 0.0;
//		if (shiji_merNum > shijichanliangNum) {
//			jiangliM = (wages - shijichanliangNum) * jiangliDJ;
//		} else if (shiji_merNum < shijichanliangNum) {
//			chengfaM = (shijichanliangNum - shiji_merNum) * chengfaDJ;
//		}
//
//		double allRewad = jiangliM;
//		double allfund = chengfaM;
//
//		for (int z = 0; z < personlength; z++) {
//			HrPerson hpObject = this.getHrPersonService().getHrPersonDao(
//					lengthss[z], "印刷组");
//			if (getCurrentDate().equals(hpObject.getRecordMonth())) {
//				hpObject.setRewardNum(hpObject.getRewardNum() + allRewad);
//				hpObject.setPoundNum(hpObject.getPoundNum() + allfund);
//				hpObject.setRecordMonth(getCurrentDate());
//				this.getHrPersonService().saveOrHrPersonDao(hpObject);
//			} else {
//				hpObject.setRewardNum(allRewad);
//				hpObject.setPoundNum(allfund);
//				hpObject.setRecordMonth(getCurrentDate());
//				this.getHrPersonService().saveOrHrPersonDao(hpObject);
//			}
//			EarningSal es = new EarningSal();
//			es.setHrDepartment("印刷组");
//			es.setHrName(lengthss[z]);
//			es.setRewardNum(allRewad);
//			es.setPoundNum(allfund);
//			es.setCurrentDate(getCurrentDate());
//			this.getEarningSalService().save(es);
//		}

		/**
		 * 工资单计算------------------------------------------------------end
		 */

		/**
		 * 工资单计算-------------------end
		 */

	}

	public PrintingTeam getPrintingTeam() {
		return printingTeam;
	}

	public void setPrintingTeam(PrintingTeam printingTeam) {
		this.printingTeam = printingTeam;
	}

	public PrintingTeamService getPrintingTeamService() {
		return printingTeamService;
	}

	public void setPrintingTeamService(PrintingTeamService printingTeamService) {
		this.printingTeamService = printingTeamService;
	}

	public Printing getPrinting() {
		return printing;
	}

	public void setPrinting(Printing printing) {
		this.printing = printing;
	}

	public PrintingService getPrintingService() {
		return printingService;
	}

	public void setPrintingService(PrintingService printingService) {
		this.printingService = printingService;
	}

	public ProvideRegimeService getProvideRegimeService() {
		return provideRegimeService;
	}

	public void setProvideRegimeService(
			ProvideRegimeService provideRegimeService) {
		this.provideRegimeService = provideRegimeService;
	}

	public ProvideRegime getProvideRegime() {
		return provideRegime;
	}

	public void setProvideRegime(ProvideRegime provideRegime) {
		this.provideRegime = provideRegime;
	}

	public HrPerson getHrPerson() {
		return hrPerson;
	}

	public void setHrPerson(HrPerson hrPerson) {
		this.hrPerson = hrPerson;
	}

	public HrPersonService getHrPersonService() {
		return hrPersonService;
	}

	public void setHrPersonService(HrPersonService hrPersonService) {
		this.hrPersonService = hrPersonService;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.prs.crm.service.cdxh.PrintingTeamService#saveALLPrintingTeamPrint
	 * (com.prs.crm.domain.cdxh.printing.PrintingTeam, java.lang.String[],
	 * java.lang.String[], java.lang.String[], java.lang.Integer[],
	 * java.lang.Integer[], java.lang.Integer[], java.lang.String[],
	 * java.lang.String[], java.lang.String[], java.lang.String[],
	 * java.lang.String[], java.lang.String[], java.lang.Integer[],
	 * java.lang.String[], java.lang.String[], java.util.Date[],
	 * java.lang.String[])
	 */
	@Override
	public void saveALLPrintingTeamPrint(PrintingTeam printingTeam,
			String[] print_size, String[] print_color, String[] packway,
			Integer[] print_order_numg, Integer[] print_articles,
			Integer[] print_shiji_num, String[] print_fangxian,
			String[] print_jianxian, String[] print_baozhuang,
			String[] print_customer, String[] print_shijian,
			String[] print_remark, Integer[] firstYN, String[] printOrderPlans,
			String[] printCodes, Date[] tihuodates, String[] rushOrders) {

		/**
		 * 打印编号
		 */
		String CodeNumbers = "04070020101202Y1";
		SerialNumberss snObject = this.getSerialNumberssService()
				.getSerialNumberss("印刷组打印");
		if (snObject == null) {
			SerialNumberss snmm = new SerialNumberss();
			snmm.setStyCategory("印刷组打印");
			snmm.setStyNumberCode(CodeNumbers);
			this.getSerialNumberssService().save(snmm);
		} else {
			String codeF = snObject.getStyNumberCode();
			String DF[] = codeF.split("Y");
			int lastCode = Integer.parseInt(DF[1]) + 1;
			CodeNumbers = DF[0] + "Y" + lastCode;
			snObject.setStyNumberCode(CodeNumbers);
			this.getSerialNumberssService().setNumCode(snObject);
		}
		int lengths = printCodes.length;
		PrintPrintIng ppObject = new PrintPrintIng();
		ppObject.setPrintCodes(CodeNumbers);
		ppObject.setPrintMachine(printingTeam.getPrintMachine());
		ppObject.setPrintClass(printingTeam.getPrintClass());
		ppObject.setPrintDate(printingTeam.getPrintDate());
		ppObject.setPrintCDateA(printingTeam.getPrintCDateA());
		ppObject.setPrintCDateB(printingTeam.getPrintCDateB());
		ppObject.setPrintCDateC(printingTeam.getPrintCDateC());
		ppObject.setPrintCDateD(printingTeam.getPrintCDateD());
		ppObject.setPrintCDateE(printingTeam.getPrintCDateE());
		ppObject.setPrintCDateF(printingTeam.getPrintCDateF());
		ppObject.setPrintCDateG(printingTeam.getPrintCDateG());
		ppObject.setPrintCDateH(printingTeam.getPrintCDateH());
		ppObject.setPrintWorkState(printingTeam.getPrintWorkState());
		ppObject.setPrintRemarks(printingTeam.getPrintRemarks());
		ppObject.setPrintZhuG(printingTeam.getPrintZhuG());
		ppObject.setPrintJishuYuan(printingTeam.getPrintJishuYuan());
		ppObject.setPrintTianBiao(printingTeam.getPrintTianBiao());
		for (int y = 0; y < lengths; y++) {
			PrintPrintIngDetail fg = new PrintPrintIngDetail();
			fg.setPrintSize(print_size[y]);
			fg.setPrintColor(print_color[y]);
			fg.setPrintPackWay(packway[y]);
			fg.setPrintOrderNum(print_order_numg[y]);
			fg.setPrintBanchenNum(print_articles[y]);
			if (print_shiji_num[y] == null) {
				fg.setPrintYSNum(0);
			} else {
				fg.setPrintYSNum(print_shiji_num[y]);
			}

			fg.setPrintFanfanxian(print_fangxian[y]);
			fg.setPrintJianxian(print_jianxian[y]);
			fg.setPrintBaozhuang(print_baozhuang[y]);
			fg.setPrintCustomer(print_customer[y]);
			fg.setPrintchanDate(print_shijian[y]);
			fg.setPrintRemark(print_remark[y]);
			ppObject.addDetail(fg);
		}
		this.getPrintPrintIngService().save(ppObject);

	}

	public PrintPrintIng getPrintPrintIng() {
		return printPrintIng;
	}

	public void setPrintPrintIng(PrintPrintIng printPrintIng) {
		this.printPrintIng = printPrintIng;
	}

	public PrintPrintIngService getPrintPrintIngService() {
		return printPrintIngService;
	}

	public void setPrintPrintIngService(
			PrintPrintIngService printPrintIngService) {
		this.printPrintIngService = printPrintIngService;
	}

	public SerialNumberssService getSerialNumberssService() {
		return serialNumberssService;
	}

	public void setSerialNumberssService(
			SerialNumberssService serialNumberssService) {
		this.serialNumberssService = serialNumberssService;
	}

	public Cdstock getCdstock() {
		return cdstock;
	}

	public void setCdstock(Cdstock cdstock) {
		this.cdstock = cdstock;
	}

	public CdstockService getCdstockService() {
		return cdstockService;
	}

	public void setCdstockService(CdstockService cdstockService) {
		this.cdstockService = cdstockService;
	}

	public DistributionOrder getDistributionOrder() {
		return distributionOrder;
	}

	public void setDistributionOrder(DistributionOrder distributionOrder) {
		this.distributionOrder = distributionOrder;
	}

	public DistributionOrderService getDistributionOrderService() {
		return distributionOrderService;
	}

	public void setDistributionOrderService(
			DistributionOrderService distributionOrderService) {
		this.distributionOrderService = distributionOrderService;
	}

	public StockPrecautionaryService getStockPrecautionaryService() {
		return stockPrecautionaryService;
	}

	public void setStockPrecautionaryService(
			StockPrecautionaryService stockPrecautionaryService) {
		this.stockPrecautionaryService = stockPrecautionaryService;
	}

	@Override
	public void saveOrPrintingTeam(PrintingTeam kk) {
		// TODO Auto-generated method stub

	}

	@Override
	public List<PrintingTeam> getPrintingTeamOrderpLan(String orderplan) {

		return this.getDao().getPrintingTeamOrderpLan(orderplan);
	}

	@Override
	public PrintingTeam getPrintingTeam(String palCode, String proCode) {

		return this.getDao().getPrintingTeam(palCode, proCode);
	}

	public static String getCurrentDate() {
		Calendar cal = Calendar.getInstance();
		int year = cal.get(Calendar.YEAR);
		int month = cal.get(Calendar.MONTH) + 1;
		int days=cal.get(Calendar.DATE);   
		return days+"/"+month + "/" + year;
	}

	public EarningSalService getEarningSalService() {
		return earningSalService;
	}

	public void setEarningSalService(EarningSalService earningSalService) {
		this.earningSalService = earningSalService;
	}

	public DistributionOrderDetailService getDistributionOrderDetailService() {
		return distributionOrderDetailService;
	}

	public void setDistributionOrderDetailService(
			DistributionOrderDetailService distributionOrderDetailService) {
		this.distributionOrderDetailService = distributionOrderDetailService;
	}

	public PrintWagesService getPrintWagesService() {
		return printWagesService;
	}

	public void setPrintWagesService(PrintWagesService printWagesService) {
		this.printWagesService = printWagesService;
	}
	String replaceCodes(String codes){
		String rr[]=codes.split("LH");
		String xx="";		
		if(rr.length==1){
			xx=codes;
		}else{
			xx=rr[1];
		}		
		return xx;
	}

	public CrmCustomerService getCrmCustomerService() {
		return crmCustomerService;
	}

	public void setCrmCustomerService(CrmCustomerService crmCustomerService) {
		this.crmCustomerService = crmCustomerService;
	}

	public cd_ProductService getCdProductService() {
		return cdProductService;
	}

	public void setCdProductService(cd_ProductService cdProductService) {
		this.cdProductService = cdProductService;
	}

	public SaleOrderService getSaleOrderService() {
		return saleOrderService;
	}

	public void setSaleOrderService(SaleOrderService saleOrderService) {
		this.saleOrderService = saleOrderService;
	}
	
}
